﻿/**
 * MotorField
 *
 * @author		Allen Chou
 * @version 	1.1.1 (last update: Aug 26 2008)
 * @link        http://cjcat.blogspot.com
 * @link		http://cjcat2266.deviantart.com
 */

/*
 * Copyright 2008 (C) Allen Chou (cjcat2266@gmail.com)
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package idv.cjcat.display.particles {
	import idv.cjcat.geom.Vector2;
	
	public class MotorField extends AbstractField {
		
		public var x:Number;
		public var y:Number;
		public var strengthN:Number;
		public var strengthT:Number;
		public var attenuation:Boolean;
		
		public function MotorField(x:Number = 0, y:Number = 0, strengthT:Number = 1, strengthN:Number = 0, attenuation:Boolean = true) {
			this.x = x;
			this.y = y;
			this.strengthN = strengthN;
			this.strengthT = strengthT;
			this.attenuation = attenuation;
		}
		
		override public function getVectorObj(x:Number, y:Number):Object {
			var rs:Number = Math.pow(x - this.x, 2) + Math.pow(y - this.y, 2);
			if (rs == 0) return { x:0, y:0 };
			var ang:Number = Math.atan2(x - this.x, y - this.y) + Math.PI / 2;
			var vecN:Vector2 = new Vector2(Math.cos(ang), -Math.sin(ang));
			var vecT:Vector2 = vecN.unitVec().rotate(-90, false);
			
			vecN.length = -strengthN / 10;
			vecT.length = strengthT;
			var vec:Vector2 = vecN.add(vecT);
			vec.length *= Math.sqrt(rs) / 100 + 0.005;
			
			if (attenuation) return { x:-5000 * vec.x / (rs + 1), y:-5000 * vec.y / (rs + 1) };
			return { x:-10 * vec.x, y:-10 * vec.y};
		}
		
	}
}